package com.itheima.admin.listener;

import com.itheima.admin.service.WemediaNewsAutoScanService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@Slf4j
public class WemediaNewsAutoScanListener {
     @Autowired
     private WemediaNewsAutoScanService wemediaNewsAutoScanService;

    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_AUTO_SCAN_TOPIC)
    public void autoScanWemediaNews(ConsumerRecord<String,String> msg){
        //生成uuid用于slf4j记录
        String uuid = UUID.randomUUID().toString();

        log.info("自动审核开始:{},{},{},{}",uuid,msg.value(),msg.topic(),msg.partition());


        try {
           wemediaNewsAutoScanService.autoScanWemediaNewsById(Long.valueOf(msg.value()),uuid);
        } catch (NumberFormatException e) {
            log.error("文章审核失败:{},{},{},{}",uuid,msg.value(),msg.topic(),msg.partition());
        }


        log.info("文章审核结束:{},{},{},{}",uuid,msg.value(),msg.topic(),msg.partition());
    }
}
